package com.water.freemarker;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerUtil {
	final static Logger log = LoggerFactory.getLogger(FreeMarkerUtil.class);

	public static void load(String path) {
		String freemarker = path + "/freemarker";
		log.info("设置FreeMarker文件目录为：" + freemarker);
		FreeMarkerConfigWraper config = FreeMarkerConfigWraper.getInstance();
		try {
			config.load(freemarker);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public static void print(Writer out, String template, Map data) {
		Template temp = null;
		try {
			temp = FreeMarkerConfigWraper.getInstance().getTemplate(template);
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		try {
			temp.process(data, out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void print(OutputStream out, String template, Map data) throws IOException, TemplateException {
		Template temp = null;
		temp = FreeMarkerConfigWraper.getInstance().getTemplate(template);
		OutputStreamWriter writer;
		writer = new OutputStreamWriter(out, "utf-8");
		temp.process(data, writer);
	}
}
